<template>
	<view class="person-msg">
		<u-navbar :background="navObj" :is-back="false" :is-fixed="true" height="80" width="100%">
			<div class="title">个人资料</div>
		
			<view class="left-box" @tap="onClickLeft">
				<u-icon name="arrow-left" size="40" color="black"></u-icon>
			</view>
		</u-navbar>
		
		<view class="head c-content" @click="changeFace">
			<image :src="form.face || '/static/newImg/touxiang.png'" mode=""></image>
			<img src="/static/newImg/zhaoxiang.png" alt="" style="width: 44rpx;height: 44rpx;" class="xiangji">
		</view>
		<u-form :model="form" ref="uForm" class="form">
			<u-form-item label="昵称" label-width="150">
				<u-input v-model="form.nickName" placeholder="请输入昵称" />
			</u-form-item>
		</u-form>
		<div class="bottom">
			<view class="submit" @click="submit">保存</view>
			<view class="submit light" @click="quiteLoginOut">退出登录</view>
		</div>
	</view>
</template>
<script>
	import {
		saveUserInfo,
		getUserInfo
	} from "@/api/members.js";
	import {
		upload
	} from "@/api/common.js";
	import storage from "@/utils/storage.js";
	import uFormItem from "@/uview-ui/components/u-form-item/u-form-item.vue";
	import city from "@/components/m-city/m-city.vue";
	export default {
		components: {
			uFormItem,
			"m-city": city
		},
		data() {
			return {
				navObj: {
					backgroundColor: '#fff',
				},
				lightColor: this.$lightColor, //高亮颜色
				form: {
					nickName:  "",
					face:  "/static/newImg/touxiang.png", //默认头像
				},
				photo: [{
						text: "立即拍照",
						color: this.$mainColor
					},
					{
						text: "从相册选择",
						color: this.$mainColor
					},
				],

			};
		},
		
		mounted() {
			
			this.getUserInfo()
		},
		
		methods: {
			
			onClickLeft() {
				uni.navigateBack();
			},
			/**
			 * 退出登录
			 */
			quiteLoginOut() {
				this.$options.filters.quiteLoginOut();
			},
			
			getUserInfo(){
				getUserInfo({
					id: storage.getUserInfo().id
				}).then(res => {
					
					this.form.nickName = res.data.name,
					this.form.face = res.data.image,
					storage.setUserInfo({...res.data})
					
				})
			},



			/**
			 * 提交保存
			 */
			submit() {
				let params = JSON.parse(JSON.stringify(this.form));
				saveUserInfo({
					userId: storage.getUserInfo().id,
					image: params.face,
					name: params.nickName
				}).then((res) => {
					if(res.code == '000000'){
						uni.showToast({
							icon:"none",
							title:'修改成功'
						})
						this.getUserInfo()
					}
					
				});
			},

			/**
			 * 修改头像
			 */
			changeFace(index) {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: upload,
							filePath: tempFilePaths[0],
							name: "file",
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data);

								this.form.face = data.data;
							},
						});
					},
				});
			},
		},
	};
</script>
<style>
	page {
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	
	.title {
		position: absolute;
		top: 65rpx;
		left: 50%;
		transform: translateX(-50%);
		font-family: PingFang;
		font-weight: bold;
		font-size: 33rpx;
		color: black;
	
	}
	
	.left-box {
		padding-left: 41rpx;
	}
	.submit {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		margin-top: 90rpx;

		width: 100%;
		margin: 0 auto;
		color: $main-color;
		border-radius: 100px;
	}

	.head {
		height: 260rpx;
		color: $font-color-light;
		font-size: $font-sm;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		line-height: 2em;

		image {
			width: 144rpx;
			height: 144rpx;
			border-radius: 50%;
		}
	}
	
	.xiangji {
		position: relative;
		top: -42rpx;
		left: 42rpx;
	}

	/deep/ .u-form {
		background-color: #ffffff;
		padding: 0;
		margin-top: 30rpx;

		.u-form-item {
			padding: 0 20rpx;
			height: 110rpx;
			line-height: 110rpx;
		}
	}

	.form {
		background-color: #ffffff;
	}

	.bottom {
		position: fixed;
		bottom: 40px;
		display: flex;
		align-items: center;
		width: 100%;

		>.submit {
			background: $light-color;
			color: #fff;
			width: 40%;
		}

	}

	.light {
		background: rgba($color: $light-color, $alpha: 0.2) !important;
		color: $light-color !important;
	}
	
	.c-content {
		border: none !important;
	}
</style>
